package com.lc.pianostudent.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.lc.pianostudent.R;
import com.lc.pianostudent.activity.ContactDetailActivity;
import com.lc.pianostudent.activity.TeacherDetailActivity;
import com.lc.pianostudent.bean.ContactListBean;
import com.lc.pianostudent.view.SelectPopupWindowNoPosition;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class ContactItemAdapter extends RecyclerView.Adapter<ContactItemAdapter.ViewHolder>{


    private Activity context;

    private List<ContactListBean> list = new ArrayList<>();
    private int parent_pos;
    private SelectPopupWindowNoPosition selectPopupWindowNoPosition;

    public ContactItemAdapter(Activity context, List<ContactListBean> list ,int parent_pos) {
        this.context = context;
        this.list = list;
        this.parent_pos = parent_pos;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position, int parent_pos);

    }

    private ContactItemAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(ContactItemAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        ContactItemAdapter.ViewHolder viewHolder = new ContactItemAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(parent_pos == 0){
            holder.username.setText(list.get(0).getCname().substring(0,1)+"老师");
            holder.charm_index.setVisibility(View.INVISIBLE);
            holder.remark.setVisibility(View.GONE);
        }else if(parent_pos == 1){
            holder.username.setText("我");

            holder.charm_index.setVisibility(View.VISIBLE);
            holder.remark.setVisibility(View.VISIBLE);

            holder.remark.setText(list.get(position).getPerson());

            holder.charm_index.setText("魅力值 "+list.get(position).getCharm());
        }else {
            holder.charm_index.setVisibility(View.VISIBLE);
            holder.remark.setVisibility(View.VISIBLE);
            holder.username.setText(list.get(position).getName()+"( "+list.get(position).getCname()+" )");

            holder.remark.setText(list.get(position).getPerson());

            holder.charm_index.setText("魅力值 "+list.get(position).getCharm());


        }

        Glide.with(context).load(list.get(position).getAvatar()).into(holder.contact_img);

        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final ContactItemAdapter.ViewHolder holder, final  int position) {


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(parent_pos == 0){
                    context.startActivity(new Intent(context, TeacherDetailActivity.class).putExtra("type","1").putExtra("id",list.get(position).getId()));
                } else  if(parent_pos == 1){
                    context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("type","0").putExtra("id",list.get(position).getId()));
                }else {
                    Log.e("----id",list.get(position).getId()+"==id");
                    context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("type","1").putExtra("id",list.get(position).getId()));

                }
            }
        });

        holder.contact_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> headList= new ArrayList<String>();
                headList.add(list.get(position).getAvatar());
                selectPopupWindowNoPosition = new SelectPopupWindowNoPosition(context,headList,position);
                //显示窗口 //设置layout在PopupWindow中显示的位置
                selectPopupWindowNoPosition.showAtLocation(holder.username, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

    }
    @Override
    public int getItemCount() {
        Log.e("=====list","list_size"+list.size());
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView username,remark,charm_index,line;
        public ImageView contact_img;


        public ViewHolder(View itemView) {
            super(itemView);

            charm_index = (TextView) itemView.findViewById(R.id.charm_index);
            remark = (TextView) itemView.findViewById(R.id.contact_remark);
            username = (TextView) itemView.findViewById(R.id.contact_username);
            line = (TextView) itemView.findViewById(R.id.line);
            contact_img = itemView.findViewById(R.id.contact_img);


        }
    }
}
